<?php
class Common extends Controller {

	function Common(){
		parent::Controller();	
		$this->load->model("M_Common","common");
	}
	function index(){
		$data["title"] = "InvoiceSystem";
		$this->load->view('welcome_message', $data);
	}	
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to display json table data 
	function getTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request		= false;
		$saferequest 	= $this->input->post('saferequest');
				
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/getReference/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------				

		$table 				= $this->input->post('table');
		$qsearch			= json_decode($this->input->post("fields"));
		$s_limit 			= $this->input->post("start");
		$e_limit 			= $this->input->post("limit");
		$dir 				= $this->input->post("dir");
		$sort 				= $this->input->post("sort");
		$qsearchlikestring	= $this->input->post("query");
		$cydep				= $this->input->post('cyDep');
		$where				= "";
		
		switch ($table){
			case 'incasari':
				$where		= "FK_FACTURI = '".$this->input->post('FK_FACTURI')."'";
			break;
		}
			
		//Data source table name with uppercase
		echo json_encode($this->common->mSelectTable($table,$s_limit,$e_limit,$sort,$dir,$qsearch,$qsearchlikestring,$cydep,$where));
	}
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to display json bills data 
	function getBillTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/getBillTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------				
		$table 				= 'facturi';
		$qsearch			= json_decode($this->input->post("fields"));
		$s_limit 			= $this->input->post("start");
		$e_limit 			= $this->input->post("limit");
		$dir 				= $this->input->post("dir");
		$sort 				= $this->input->post("sort");
		$qsearchlikestring	= $this->input->post("query");
		
		//Data source table name with uppercase
		echo json_encode($this->common->mSelectBillTable($s_limit,$e_limit,$sort,$dir,$qsearch,$qsearchlikestring));
	}
	function getNotPayedBillsTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/getNotPayedBillsTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------	
		$table 				= $this->input->post('table');			
		$s_limit 			= $this->input->post("start");
		$e_limit 			= $this->input->post("limit");
		$dir 				= $this->input->post("dir");
		$sort 				= $this->input->post("sort");
		$qsearch			= json_decode($this->input->post("fields"));
		$qsearchlikestring	= $this->input->post("query");
		
		//Data source table name with uppercase
		echo json_encode($this->common->mSelectNotPayedBills($excel=false,$table, $s_limit,$e_limit,$sort,$dir,$qsearch,$qsearchlikestring));
	}		
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to display json reference data by family code 
	function getReference(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/getReference/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------		
		
		$code = $this->input->post('code');
		
		$response = array();
		$response["DS_".strtoupper($code)]	= $this->common->mSelectReference($code);
		
		echo json_encode($response);
	}	
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to select table data
	function selectTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/selectTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------			

		$table 		= $this->input->post('table');
		//company dependent
		$cyDep		= $this->input->post('cyDep');
		//active
		$active		= $this->input->post('active');
		
		$cyDep		= (isset($cyDep) && !empty($cyDep)) ? $cyDep : false;
		$active		= (isset($active) && !empty($active)) ? $active : false;
				
		$response 	= array();
		$response["DS_".strtoupper($table)] = $this->common->mSelect($table,$cyDep,$active);
		
		echo json_encode($response);
	}
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to select table data
	function selectProduseTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/selectProduseTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------			

		$table 		= $this->input->post('table');
		//company dependent
		$cyDep		= $this->input->post('cyDep');
		//active
		$active		= $this->input->post('active');
		
		$cyDep		= (isset($cyDep) && !empty($cyDep)) ? $cyDep : false;
		$active		= (isset($active) && !empty($active)) ? $active : false;
				
		$response 	= array();
		$response["DS_".strtoupper($table)] = $this->common->mSelectProduse($table,$cyDep,$active);
		
		echo json_encode($response);
	}	
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to insert data into table 	
	function insertTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/insertTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
		$sess 		= $this->session->userdata('us_');
		$cId 		= $sess['us_C'];
		$data 		= array();
								
		$table = $this->input->post('table');
		//company dependent
		$cyDep		= $this->input->post('cyDep');
		$cyDep		= (isset($cyDep) && !empty($cyDep)) ? $cyDep : false;
				
		$commonData=array(
							'saferequest' 	=> $this->input->post('saferequest'),
							'table' 		=> $this->input->post('table'),
							'cyDep'			=> $cyDep
		);

		//excluding ajax call variables 				
		$neededData = array_diff_assoc($_POST,$commonData);

		foreach($neededData as $key => $value){
		      	$data[$key]=$value;
		}
		
		if ($cyDep == true){
			$data['COMPANY_ID'] = $cId;
		}
		
		if($this->common->mInsert($table,$data) == true){   
		//echo '{"success":false,"errors":{"nr_reg":"O tamanho do nome deve ter menos do que 10 caracteres"}} ';   
			echo json_encode(array(
			        'success'=> true,
			        'code' => 1
			    ));
	    		      		
	    }else{
			echo json_encode(array(
			        'success'=> true,
			        'code' => 0
			    ));  
	    }		
	}
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to update table
	function updateTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/updateTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
				
		$table 	= $this->input->post('table');
		$id 	= $this->input->post('ID');
		
		$commonData=array(
							'saferequest' => $this->input->post('saferequest'),
							'ID' => $this->input->post('ID'),
							'table' => $this->input->post('table'),
						);
		$neededData = array_diff_assoc($_POST,$commonData);

		foreach($neededData as $key => $value){
		      	$data[$key]=$value;
		}
		
		if($this->common->mUpdate($table,$id,$data) == true){      
			echo json_encode(array(
			        'success'=> true,
			        'code' => 1
			    ));  		
	    }else{
			echo json_encode(array(
			        'success'=> true,
			        'code' => 0
			    ));  
	    }
	    		
	}
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to delete table	records [one or array(ids)]	
	function deleteTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/deleteTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
				
		$table = $this->input->post('table');
		$ids = json_decode(stripslashes($this->input->post('ids')));
		
	    if(sizeof($ids)<1){
	      echo "0";
	    } else if (sizeof($ids) == 1){
	    	if($this->common->mDelete($table,$ids[0]) == true){
	    		echo "1";
	    	}
	    } else {
	    	$checked = "1";
			for($i = 0; $i < sizeof($ids); $i++){
			 	if($this->common->mDelete($table,$ids[$i]) != true){
			 		$cheched = "0";
			 	}
	      	}
			echo $checked;
	    }
	}
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to disable table records [one or array(ids)]	
	function enableDisableTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/enableDisableTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
				
		$table = $this->input->post('table');
		$ids = json_decode(stripslashes($this->input->post('ids')));
		$action = $this->input->post('action');
		
	    if(sizeof($ids)<1){
	      echo "0";
	    } else if (sizeof($ids) >= 1){
	    	$checked = "1";		
			switch ($action){
				case 'disable':
					for($i = 0; $i < sizeof($ids); $i++){
					 	if($this->common->mEnableDisable($table,$ids[$i],$action) != true){
				    		$cheched = "0";
				    	}
				 	}
					echo $checked;				
				break;
				case 'enable':
					for($i = 0; $i < sizeof($ids); $i++){
					 	if($this->common->mEnableDisable($table,$ids[$i],$action) != true){
				    		$cheched = "0";
				    	}
				 	}
					echo $checked;				
				break;				
			}
	    } 
	}	
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to load record form data
	function selectTableWhere(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/selectTableWhere/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
		
		$this->load->library('ext_form_loading_array');
		
		$table = $this->input->post('table');
		$record = $this->input->post('ID');

		if($this->common->mSelectWhere($table,$record)){
			
			$array = $this->common->mSelectWhere($table,$record);
			$str = $this->ext_form_loading_array->build($array);
			echo $str;
		}
	}
	function selectTableWhereByCompany(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/selectTableWhere/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
		
		$this->load->library('ext_form_loading_array');
		
		$table = $this->input->post('table');

		if($this->common->mSelectWhereByCompany($table)){
			
			$array = $this->common->mSelectWhereByCompany($table);
			$str = $this->ext_form_loading_array->build($array);
			echo $str;
		}
	}	
	/*
	 * 2008 - iello-dev 
	 * gp
	 * 
	 */
	//function used to validate form fields data - CI validations	
	function validateTable(){
		/*
		 * put this AJAX safe request check in every CI function called by AJAX 
		 * 
		 */
		//verify AJAX request[saferequest = true]
		$request= false;
		$saferequest = $this->input->post('saferequest');
		
		$request = (!empty($saferequest) && ($saferequest == true)) ? true : false; 
		$url = $this->config->config['base_url']."common/validateTable/";
		$this->ajax_request->_check_request($url,$request);
		//--------------------------------------------------------------------------------
		
		$this->load->library('validation');
		
		switch ($this->input->post('table')){
			case 'll_users' :
				switch($this->input->post('field')){
					case 'name':
						$rules['value'] = "required|min_length[5]";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Minim 5 caractere</i>
									";
						break;
					case 'surname':
						$rules['value'] = "required|min_length[5]";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Minim 5 caractere</i>
									";
						break;			
					case 'user':
						$rules['value'] = "required|min_length[5]";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Minim 5 caractere</i>
									";
						break;	
					case 'cnp':
						$rules['value'] = "required|min_length[5]";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Minim 5 caractere</i>
									";
						break;		
					case 'pass':
						$rules['value'] = "required|min_length[5]";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Minim 5 caractere</i>
									";
						break;																					
				}		
			break;
			case 'clienti':
				switch($this->input->post('field')){
					case 'n_tip':
						$rules['value'] = "required";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> 
									";
						break;
					case 'email':
						$rules['value'] = "valid_email";
						$message = "
									Reguli validare: <br />
									<i>Email invalid</i> 
									";
						break;						
					case 'cnp':
						$rules['value'] = "required|numeric";
						$message = "
									Reguli validare: <br />
									<i>Camp obligatoriu</i> <br /> 
									<i>Camp numeric</i>
									";
						break;												
				}
			break;
		}
				
		$this->validation->set_rules($rules);
		
		if ($this->validation->run() == FALSE){
			echo json_encode(array(
			        'success'=> true,
			        'valid' => false,
					'reason'=> $message
			    ));  
		}else{
			echo json_encode(array(
			        'success'=> true,
			        'valid' => true
			    ));  
		}
	}	
}
?>
